﻿using System;
using System.Data;
using System.Data.Common;
using QzEbook.Common;
using QzEbook.Common.Emum;

/// <summary>
///UserInfo 的摘要说明
/// </summary>
/// 
namespace QzEbook.AccessObject
{
    public class SiteUser : abstractAccess<SiteUser>
    {
        private string name;
        private int uid;
        private bool sex;
        private string qq;
        private string ip;
        private string tel;

        private DateTime date;
        private string levelName;
        private string pwd;

        private double quota;
        private int quotaBooks;

        private double gross;
        private int grossBooks;

        public SiteUser()
        {
        }

        #region 属性;

        public double Quota
        {
            get { return quota; }
            set { quota = value; }
        }
        public int QuotaBooks
        {
            get { return quotaBooks; }
            set { quotaBooks = value; }
        }
        public double Gross
        {
            get { return gross; }
            set { gross = value; }
        }
        public int GrossBooks
        {
            get { return grossBooks; }
            set { grossBooks = value; }
        }

        public bool Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        private string from;

        public string From
        {
            get { return from; }
            set { from = value; }
        }
        public string Ip
        {
            get { return ip; }
            set { ip = value; }
        }
        private string mail;

        public string Mail
        {
            get { return mail; }
            set { mail = value; }
        }
        public string Tel
        {
            get { return tel; }
            set { tel = value; }
        }
        public string Qq
        {
            get { return qq; }
            set { qq = value; }
        }
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        public int Uid
        {
            get { return uid; }
            set { uid = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }

        public string LevelName
        {
            get { return levelName; }
            set { levelName = value; }
        }
        #endregion



        public static bool check(RegCheckAction action,string value)
        {
            int i = 0;
            switch (action)
            {
                case RegCheckAction.mail:
                    i=int.Parse(DataAccess.ExcuteScalar(String.Format("SELECT Count(UID) FROM Qz_User WHERE UMail='{0}'", value)));
                    break;
                case RegCheckAction.name:
                    i = int.Parse(DataAccess.ExcuteScalar(String.Format("SELECT Count(UID) FROM Qz_User WHERE UName='{0}'", value)));
                    break;
                default:
                    break;
            }
            //System.Web.HttpContext.Current.Response.Write(i.ToString());
            return (i ==0);
        }

        /// <summary>
        /// 绑定到列;
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        /// 
        protected override SiteUser bindRow(DataRow row)
        {
            SiteUser user = new SiteUser();
            user.Uid = int.Parse(row["UID"].ToString());
            user.Name = row["UName"].ToString();
            user.LevelName = row["ULevel"].ToString();

            user.Quota = double.Parse(row["UQuota"].ToString()) / 1024;
            user.QuotaBooks = int.Parse(row["UQuotaBooks"].ToString());

            user.Gross = Math.Round(double.Parse(row["UGross"].ToString()) / 1024, 2);
            user.GrossBooks = int.Parse(row["UGrossBooks"].ToString());

            user.Sex = bool.Parse(row["USex"].ToString());
            user.From = row["UFrom"].ToString();
            user.Mail = row["UMail"].ToString();
            user.Qq = row["UQq"].ToString();
            user.Date = DateTime.Parse(row["UDate"].ToString());

            return user;
        }

        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("SelectUser");
            addParameter(command, "@UID", Uid);
            return DataAccess.ExcuteSelectCommand(command);
        }

        public override bool insert()
        {
            DbCommand command = CreateCommand("RegistUser");
            addParameter(command, "@UName", Name);
            addParameter(command, "@UPwd", Pwd);
            addParameter(command, "@USex", Sex);
            addParameter(command, "@UFrom", From);
            addParameter(command, "@UMail", Mail);
            addParameter(command, "@UQq", Qq);
            addParameter(command, "@UTel", Tel);
            

            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
            }
            catch {
            }
            return (i != -1);
        }
        public override bool update()
        {
            DbCommand command = CreateCommand("UpdateUserInfo");
            addParameter(command, "@UID", Uid);
            addParameter(command,"@UQq", Qq);
            addParameter(command,"@UFrom", From);
            addParameter(command,"@USex", Sex);
            int i = -1;
            try
            {
                i = DataAccess.ExecuteNonQuery(command);
            }catch{
            }
            return ( i!= -1);
        }
    }
}
